<template>
	<view class="page">
		<view class="page-box">
			<view class="btnBox">
				<image class="tex" src="@/static/login/register.png" mode=""></image>
				<view class="choose-nickname-row" style="display: flex;justify-content: space-between;">
					<vvSelect :list="allList" label="name" valueKey="id" placeholder-style="color: #807E7E"
						:placeholder="placeholderText" ref="vvSelect" @change="tabChange" style="width: 86%;">
					</vvSelect>
					<view class="" style="margin-left: 8rpx;">
						<u-icon name="arrow-down" size="16" v-if="!closeIcon"></u-icon>
						<u-icon name="close-circle" color="#807E7E;" v-else size="18" @click="closeSearch"
							style="margin-left: 10rpx;"></u-icon>
					</view>

				</view>
				<view class="choose-nickname-row">
					<input v-model="form.nickname" type="nickname" placeholder="请输入用户名" />
				</view>
				<view class="choose-nickname-row">
					<input v-model="form.firstPassword" type="text" placeholder="请设置密码" password />
				</view>
				<view class="choose-nickname-row">
					<input v-model="form.password" type="text" placeholder="请再次输入密码" password />
				</view>
				<view class="choose-nickname-row">
					<input v-model="form.mobile" type="number" placeholder="请输入手机号" />
				</view>
				<view class="choose-nickname-row" style="display: flex;justify-content: space-between;">
					<input v-model="form.code" placeholder="请输入验证码" />
					<view class='codeinput' @tap.stop="getCode"> 获取验证码 </view>
				</view>
				<view v-if="form.type == 2" class="choose-nickname-row" style="margin-bottom: 20rpx;">
					<input v-model="form.credit_code" placeholder="请输入信用代码" />
				</view>
				<view v-if="form.type == 2">
					<u-upload :fileList="[]" @afterRead="afterRead" @delete="deletePic" name="business_license"
						:maxCount="1">
						<button v-if="coverPath.length == 0" style="width: 640rpx;">上传营业执照</button>
						<view style="width: 100%;height: 300rpx;" v-else>
							<image v-for="(item, index) in coverPath" :key="index" :src="baseUrl + item.url"
								mode="scaleToFill" style="width: 640rpx;height: 300rpx;" />
						</view>
					</u-upload>
				</view>
			</view>
			<view class="forgetCode">
				<view class="noCode">
					<view class="">已有账号,</view>
					<view class="forgetCode" style="width: 40%;margin-top: 0;" @tap.stop="toLogin">登录?</view>
				</view>
			</view>

			<view class="proGroup">
				<view class="left">
					<u-checkbox-group @change="checkboxChange">
						<u-checkbox :checked="checked" :value="checked" shape="circle" active-color="#000"
							label-disabled>
						</u-checkbox>
					</u-checkbox-group>
				</view>
				<view class="right">
					<view class="first">
						我已阅读并同意
					</view>
					<view class="second">
						<view class="Code" @tap.stop="Jump('zcxy')">《注册协议》</view>和
						<view class="Code" @tap.stop="Jump('ysxy')">《隐私协议》</view>
					</view>
				</view>
			</view>

			<!-- :style="{background:allFill?'#466EC5':'#EBEBEB',color:allFill?'#fff':'#909090'}" -->
			<view class="button" size="mini" @tap.stop="toSubmit">注册</view>
		</view>
	</view>
</template>
<script>
	import config from "@/request/baseUrl.js";
	import WxLogin from "@/components/Author/index";
	import vvSelect from '@/components/vv-select/index.vue'
	import {
		commonUpload
	} from "@/request/common.js";
	import {
		weixinLogin,
		myProInfo,
		myPhone,
		mySendSms,
		register
	} from "@/request/myApi.js";
	export default {
		components: {
			WxLogin,
			vvSelect
		},
		data() {
			return {
				checked: false,
				baseUrl: config.baseUrl,
				showPop: false, // 完善信息
				userInfo: {}, // 用户信息
				mobile: "", // 手机号码
				token: "",
				allList: [{
						name: '机构',
						id: 2
					},
					{
						name: '个人',
						id: 1
					}
				],
				placeholderText: '请选择用户类型',
				form: {
					type: '',
					nickname: '',
					firstPassword: '',
					password: '',
					mobile: '',
					code: '',
					credit_code: '',
					business_license: '',
				},
				coverPath: [], // 临时营业执照图片
				closeIcon: false,
				allFill: false
			};
		},
		created() {},
		watch: {
			form: {
				handler(e) {
					for (var key in e) {
						// console.log(e, e[key], 'fffffffff')
						if (!e[key]) {
							this.allFill = false
						} else {
							this.allFill = true
						}
					}

					// console.log(this.allFill,'a',e)
				},
				deep: true, // 可以深度检测到 obj 对象的属性值的变化
				immediate: true //刷新加载  立马触发一次handler
			},

		},

		methods: {
			checkboxChange() {
				this.checked = !this.checked
			},
			// 跳转协议
			Jump(type) {
				uni.navigateTo({
					url: '/pages/my/agreement?name=' + type
				})
			},
			// 获取验证码
			getCode() {
				if (!this.form.mobile) {
					uni.showToast({
						title: "手机号不能为空",
						icon: "none",
					});
					return;
				}
				if (!/^1[3-9]\d{9}$/.test(this.form.mobile)) {
					uni.showToast({
						title: "手机号格式有误",
						icon: "none",
					});
					return;
				}
				mySendSms({
					mobile: this.form.mobile,
					event: "register",
				}).then((res) => {
					if (res.code == 200) {
						uni.showToast({
							title: res.msg,
							icon: "none",
						});
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
						});
					}
				});
			},

			closeSearch() {
				this.$refs.vvSelect.emptCheckLabels()
				this.form.type = ''
				this.closeIcon = false
			},
			tabChange(e) {
				this.form.type = e
				this.closeIcon = true
			},
			// 忘记密码
			toForgetCode() {
				uni.navigateTo({
					url: '/pages/bind/forgetCode'
				})
			},
			// 登录
			toLogin() {
				uni.navigateTo({
					url: '/pages/bind/login'
				})
			},
			// 关闭授权弹窗
			cencelPopup(v) {
				this.showPop = false;
			},
			// 注册
			toSubmit() {
				console.log(this.form, 'form')
				if (!this.checked) {
					uni.showToast({
						title: '请阅读勾选注册协议与隐私协议',
						duration: 1500,
						icon: 'none',
					})
					return
				}
				if (this.form.type == 2 && this.coverPath.length == 0) {
					uni.showToast({
						title: '请上传营业执照',
						icon: 'none',
					})
					return;
				}
				let data = Object.assign({}, this.form);
				if (this.form.type == 2) {
					data.business_license = this.coverPath[0].url
				}
				register(data).then((res) => {
					console.log(res, 'res')
					if (res.code == 200) {
						uni.setStorageSync('userInfo', res.data.userinfo)
						uni.setStorageSync('token', res.data.userinfo.token)
						uni.showToast({
							title: res.msg,
							icon: "none",
						});
						setTimeout(() => {
							uni.navigateTo({
								url: '/pages/bind/login'
							})
						}, 1000)
					} else {
						uni.showToast({
							title: res.msg,
							icon: "none",
						});
					}
				});
			},
			// 新增图片
			async afterRead(e) {
				let list = [].concat(e.file);
				this.coverPath = [];
				let len = this.coverPath.length;
				list.map((item) => {
					this.coverPath.push({
						...item,
						status: "uploading",
						message: "上传中",
					});
				});
				for (let i = 0; i < list.length; i++) {
					const result = await this.uploadFilePromise(list[i].url);
					let item = this.coverPath[len];
					this.coverPath.splice(
						len,
						1,
						Object.assign(item, {
							status: "success",
							message: "",
							url: result,
						})
					);
					len++;
				}
			},
			// 上传凭证图片
			uploadFilePromise(url) {
				return new Promise((resolve, reject) => {
					commonUpload(url).then((res) => {
						if (res.code == 200) {
							setTimeout(() => {
								resolve(res.data[0]);
							}, 1000);
						}
					});
				});
			},
			// 删除图片
			deletePic(e) {
				this.coverPath.splice(e.index, 1);
			},
			// 获取code
			getPhoneNumber(e) {
				let data = {
					code: e.detail.code,
				};
				myPhone(data).then((res) => {
					if (res.code == 1) {
						this.mobile = res.data.mobile;
						this.handleLogin();
					}
				});
			},
			// 完善账号
			handleBind(val) {
				let data = {
					token: this.token,
					avatarUrl: val.avatarUrl,
					nickName: val.nickname,
					mobile: this.mobile,
				};
				myProInfo(data).then((res) => {
					if (res.code == 1) {
						this.userInfo = res.data.userinfo;
						uni.setStorageSync("USER_INFO", JSON.stringify(this.userInfo));
						uni.navigateTo({
							url: "/pages/bind/bindPhone"
						});
						this.showPop = false;
					}
				});
			},
		},
	};
</script>
<style lang="scss" scoped>
	.proGroup {
		display: flex;
		margin-top: 60rpx;

		.left {
			width: 52rpx;
		}

		.right {
			flex: 1;
			display: flex;

			.first {
				color: #909090;
				font-size: 26rpx;
				width: 34%;
			}

			.second {
				display: flex;
				color: #909090;
				font-size: 26rpx;

				.Code {
					color: #466EC5;
					width: 99%;
					font-size: 26rpx;
				}
			}
		}
	}

	.page {
		background: url(@/static/bg.png);
		background-size: 100% 100%;
		height: 100vh;
		// background-repeat: no-repeat;
		// position: fixed;
		// top: 0;
		// overflow: hidden;
		// left: 0;
		width: 100vm;

		&-box {
			min-height: 910rpx;
			margin: 0 auto;
			padding: 60rpx;
			width: 80%;
			display: flex;
			flex-direction: column;
			justify-items: center;

			>view {
				margin-bottom: 0rpx;
			}

			.btn {
				font-size: 28rpx;
				background-color: #fff;
				border: 1rpx solid #dddddd;
				padding: 20rpx;
				border-radius: 8rpx;

				&:active {
					background-color: #f2f2f2;
				}
			}
		}
	}

	.button {
		background-color: #EBEBEB;
		width: 100%;
		color: #909090;
		border: 2rpx solid #EBEBEB;
		border-radius: 50rpx;
		height: 86rpx;
		text-align: center;
		line-height: 86rpx;
		margin-top: 56rpx;
		font-size: 30rpx;
	}

	.noCode {
		color: #909090;
		font-size: 26rpx;
		margin-top: 10rpx;
		display: flex;
		justify-content: space-between;
		width: 31%;
		float: right;
	}

	.forgetCode {
		color: #466EC5;
		width: 99%;
		font-size: 26rpx;
		text-align: right;
		margin-top: 0rpx;
	}

	.btnBox {
		width: 100%;
		min-height: 730rpx;
		border-top-left-radius: 50rpx;
		border-top-right-radius: 50rpx;

		.tex {
			width: 140rpx;
			height: 80rpx;
			margin-bottom: 40rpx;
		}

		.choose-nickname-row {
			width: 100%;
			height: 5%;
			text-align: left;
			padding: 26px 2px 8px;
			font-size: 28rpx;
			margin: 0 auto;
			border-bottom: 3rpx solid #F2F2F2;
			vertical-align: center;

			/deep/.input {
				color: #000;
			}

			.codeinput {
				color: #466EC5;
				width: 180rpx;
			}
		}

	}
</style>